/*
 * passwordlist.c
 *
 *  Created on: 02.06.2013
 *      Author: Eros
 */

//Passwords are separated by :
#include "passwordlist.h"

char** biggerArray(char** origarr, int count)
{
	char** tmplist = (char**) malloc((count)*sizeof(char*));
	for (int i=0; i<count; i++)
	{
		tmplist[i] = (char*)malloc(strlen(origarr[i]));
		strcpy(tmplist[i], origarr[i]);
	}
	//origarr = tmplist;
	return tmplist;
}

int main(int argc, char** argv)
{
	getPasswordList("myFile.txt");
	return 0;
}

char** getPasswordList(char* fileName)
{
	struct stat status;
	char** wordlist;
	int temp;
#ifdef __WIN32
	temp = stat(fileName, &status);
#else
	temp = lstat(fileName, &status);
#endif
	if (temp == -1){
		perror("stat");
		return NULL;
	}
	FILE* fd = fopen(fileName,"rb");
	char* fileIn = (char*)malloc(status.st_size);
	//wordlist = (char**)malloc(status.st_size);
	int size = fread(fileIn,sizeof(char), status.st_size, fd);
	if (size != status.st_size)
	{
		perror("read");
		fclose(fd);
	}
	char* pch = strtok(fileIn, ":");
	int count = 0;
	while (pch != NULL)
	{

		//free(tmplist);
		//wordlist = (char**) malloc(20*sizeof(char *));
		//for (int i=0; i<20; i++)
		//{
			//wordlist[i] = (char*)malloc(20);
			//strcpy(wordlist[i], "aaaa");
			//printf("%s\n", wordlist[i]);
		//}
		wordlist = biggerArray(wordlist, count);
		wordlist[count] = (char*)malloc(strlen(pch));
		strcpy(wordlist[count], pch);
		pch = strtok(NULL, ":");
		count++;
	}
	//exit(0);
#if (DEBUG)
	for (int k= 0;k<count; k++)
		printf("%s\n", wordlist[k]);
#endif
	fclose(fd);
	free(fileIn);
	return wordlist;
}
